home *** CD-ROM | disk | FTP | other *** search
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <intuition/intuition.h>
- #include <dos/dos.h>
-
- #include <clib/exec_protos.h>
- #include <clib/alib_protos.h>
- #include <clib/intuition_protos.h>
- #include <clib/gadtools_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/utility_protos.h>
- #include <clib/commodities_protos.h>
- #include <clib/alib_protos.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "/include/server.h"
-
- /* Pulizia di una porta messaggi */
- extern void ClearPendingMessages(struct MsgPort *);
-
- /* Controllo commodity */
- extern BOOL SetUpCommodity(int,char**);
- extern void RemoveCommodity(void);
- extern void HandleCxMessages(void);
-
- /* Gestione finestra */
- extern void DetachGadgets( void );
- extern void AttachGadgets( void );
- extern BOOL PopUpWindow(void);
- extern void ShutWindow(void);
- extern void HandleWindowMessages(void);
-
- /* Builtin blanker */
- extern BOOL PopUpBlackScreen(void);
- extern void CloseBlackScreen(void);
-
- /* Fa partire i clienti nella ClientList */
- extern BOOL StartClient( struct ClientNode * );
- extern void GetClientNames( char * );
- extern void DropClientNames( void );
-
- /* Alloca una dinfo per un cliente */
- extern struct DisplayIDInformation *AllocDisplayIDInformation( ULONG );
- extern struct Window *window;
-
- /* Per la trasmissione del displayID */
- extern ULONG DisplayID;
- extern char DefaultModeName[];
- extern struct List *CreateModeList( void );
- extern void GetDisplayNodeFromName( void );
- extern void DeleteModeList( void );
-
- extern struct IntuitionBase *IntuitionBase;
- extern struct Library *IconBase, *GadToolsBase, *UtilityBase, *CxBase;
- extern struct Gadget *list_gdg;
-
- char *version = "$VER: "PROGRAMNAME" "PROGRAMVERSION" ("PROGRAMDATE")";
-
- /* Per la notifica dei vari eventi */
- struct MsgPort *handlerPort, *timerMPort, *clientMPort;
- struct Task *thisTask;
- UBYTE handlerSigBit, timerSigBit;
- ULONG handlerSignal, timerSignal;
- extern ULONG CXSignal, windowSignal, timerSignal;
-
-
- BOOL StayCool = TRUE; /* Terminazione */
- BOOL Blanking = FALSE;
- BOOL RandomClient = TRUE; /* Deve essere scelto a caso? */
- BOOL CommodityActive = TRUE; /* Deve effettuare il blank? */
- BOOL PopUp = FALSE; /* Deve aprire la finestra? */
- UWORD timeElapsed; /* Tempo passato dall'ultimo evento */
- UWORD delaySecs, delayEvents; /* Secondi da passare trasform. in Ticks */
- UWORD DefaultClient = 0;
- UWORD TotalClients = 0;
-
- #define MUST_RETRY 0
- #define MUST_BLANK 1
-
- BOOL BlankStatus = MUST_RETRY;
-
- /* Usate nella scelta del cliente per non toccare le originali */
- /* In caso di fallimento infatti vanno modificate per scandire */
- /* la lista dalla fine all'inizio alla ricerca di un nuovo cli: */
-
- UWORD copy_of_DefaultClient = (UWORD)~0;
- BOOL copy_of_RandomClient = (UWORD)~0;
-
- #define BLANKING_NONE 0
- #define BLANKING_BUILTIN 1
- #define BLANKING_EXTERN 2
-
- /* Prototipi delle funzioni */
- struct ClientMessage *AllocClientMessage( ULONG );
- void FreeClientMessage( struct ClientMessage * );
- struct ClientNode *FindClientNode( UWORD );
- void RemoveClient( UWORD, char * );
-
- void SendCommandToClient( ULONG );
- void QuitClients( void );
- void HandleClients(void);
- struct MsgPort *SetUpServerPort(void);
- void RemoveServerPort(void);
-
- void StartBlanking(void);
- void StopBlanking(void);
-
- void ProcessUserMessages(void);
- void main(int,char**);
-
- /*************************
- * *
- * HANDLE EXTERN PROGRAMS *
- * *
- *************************/
-
- struct MsgPort *ServerPort;
- ULONG serverSignal;
- UBYTE blankingAction;
-
- struct List ClientsList;
-
-
- struct ServerMessage *AllocServerMessage( ULONG command )
- {
- struct ServerMessage *bmsg;
-
- if ( bmsg = AllocVec( sizeof(struct ServerMessage), MEMF_PUBLIC | MEMF_CLEAR ) )
- {
- bmsg->sm_Msg.mn_Node.ln_Type = NT_MESSAGE;
- bmsg->sm_Msg.mn_ReplyPort = ServerPort;
- bmsg->sm_Msg.mn_Length = sizeof(struct ServerMessage);
- bmsg->sm_Command = command;
- }
- return( bmsg );
- }
-
-
- void FreeServerMessage( struct ServerMessage *bmsg )
- {
- FreeVec( bmsg );
- }
-
-
- struct ClientNode *FindClientNode( UWORD clientNumber )
- {
- struct ClientNode *node = (struct ClientNode *)ClientsList.lh_Head;
- UWORD counter = 0;
-
- while ( counter < clientNumber )
- {
- counter++;
- node = (struct ClientNode *)node->cn_Node.ln_Succ;
- }
-
- return( node );
- }
-
-
- void RemoveClient( UWORD clientNumber, char *clientName )
- {
- struct ClientNode *node;
-
- node = FindClientNode( clientNumber );
-
- if ( node != (struct ClientNode *)ClientsList.lh_Head )
- {
- DetachGadgets();
- Remove( (struct Node *)node );
- FreeMem( node, sizeof(struct ClientNode) );
- TotalClients--;
-
- if ( clientNumber < TotalClients )
- DefaultClient = clientNumber;
- else
- DefaultClient = TotalClients;
-
- AttachGadgets();
- }
- }
-
-
- void SendCommandToClient( ULONG command )
- {
- struct ServerMessage *bmsg;
-
- if ( clientMPort && (bmsg = AllocServerMessage( command )))
- {
- PutMsg( clientMPort, (struct Message *)bmsg );
- do
- WaitPort( ServerPort );
- while ( !(GetMsg( ServerPort )) );
-
- FreeServerMessage( bmsg );
- }
- }
-
-
- void HandleClients( void )
- {
- struct ClientMessage *bmsg;
-
- while( bmsg = (struct ClientMessage *)GetMsg( ServerPort ) )
- {
- switch ( bmsg->cm_Action )
- {
- case ACTION_ARRIVED:
- if ( !Blanking )
- {
- clientMPort = bmsg->cm_Msg.mn_ReplyPort;
- bmsg->DInfo = AllocDisplayIDInformation( DisplayID );
- blankingAction = BLANKING_EXTERN;
- BlankStatus = MUST_BLANK;
- Blanking = TRUE;
- }
- break;
- case ACTION_FAILED:
- if ( Blanking )
- {
- if ( copy_of_RandomClient != 0 )
- {
- copy_of_RandomClient = 0;
- copy_of_DefaultClient = TotalClients;
- }
- else
- copy_of_DefaultClient--;
- StartBlanking();
- }
- default:
- break;
- }
- ReplyMsg( (struct Message *)bmsg );
- }
- }
-
-
- struct MsgPort *SetUpServerPort( void )
- {
- if ( ServerPort = CreatePort( SERVERPORTNAME, 0 ) )
- serverSignal = 1L << ServerPort->mp_SigBit;
- return( ServerPort );
- }
-
-
- void RemoveServerPort( void )
- {
- DeletePort( ServerPort );
- }
-
-
- /*****************
- * *
- * Blanking stuff *
- * *
- *****************/
-
- UWORD ActiveClient;
-
- void StartBlanking( void )
- {
- ULONG secs, micr;
-
- clientMPort = NULL;
-
- if ( !Blanking )
- {
- if ( copy_of_DefaultClient == ~0 )
- {
- copy_of_DefaultClient = DefaultClient;
- copy_of_RandomClient = RandomClient;
- }
-
- if ( window )
- ClearPendingMessages( window->UserPort );
-
- if ( TotalClients && copy_of_RandomClient )
- {
- CurrentTime( &secs, &micr );
- ActiveClient = secs % TotalClients + 1;
- }
- else ActiveClient = copy_of_DefaultClient;
-
- if ( ActiveClient == 0 )
- {
- if ( PopUpBlackScreen() )
- {
- blankingAction = BLANKING_BUILTIN;
- Blanking = TRUE;
- }
- else
- {
- DisplayBeep( NULL );
- blankingAction = BLANKING_NONE;
- Blanking = TRUE;
- }
- }
- else
- {
- if ( !(StartClient( FindClientNode( ActiveClient ) ) ) )
- {
- copy_of_DefaultClient = 0;
- Blanking = TRUE;
- StartBlanking();
- }
- else
- /* Blanking = TRUE <=> ACTION_ARRIVED */
- blankingAction = BLANKING_EXTERN;
- }
- }
- }
-
-
- void StopBlanking( void )
- {
- if ( blankingAction == BLANKING_BUILTIN )
- CloseBlackScreen();
- else
- if ( blankingAction == BLANKING_EXTERN )
- SendCommandToClient( COMMAND_QUIT );
-
- Blanking = FALSE;
- blankingAction = BLANKING_NONE;
-
- copy_of_DefaultClient = (UWORD)~0;
- copy_of_RandomClient = (UWORD)~0;
- }
-
-
- /*******************
- * *
- * PROCESS MESSAGES *
- * *
- *******************/
-
- void ProcessUserMessages(void)
- {
- ULONG signal;
-
- while( StayCool )
- {
- signal = Wait( CXSignal | windowSignal | serverSignal | handlerSignal | timerSignal );
-
- if ( signal & CXSignal )
- HandleCxMessages();
-
- if ( signal & windowSignal )
- HandleWindowMessages();
-
- if ( signal & serverSignal )
- HandleClients();
-
- if ( signal & timerSignal && CommodityActive )
- StartBlanking();
-
- if ( signal & handlerSignal )
- StopBlanking();
- }
-
- if ( Blanking )
- StopBlanking();
- }
-
-
- /***************
- * *
- * MAIN PROGRAM *
- * *
- ***************/
-
- #define POP_KEY_ID 100
-
- extern CxObj *broker , *sender, *translate;
- CxObj *hotkeyfilter;
- CxObj *mousefilter, *timerfilter, *keyfilter;
-
- extern struct NewBroker newbroker;
- extern struct MsgPort *broker_mp;
-
-
- void __interrupt __saveds BlankerAction(CxMsg *CxMsg,CxObj *CO)
- {
- struct InputEvent *IE=(struct InputEvent *)CxMsgData(CxMsg);
-
- if (IE->ie_Class==IECLASS_TIMER)
- {
- if ( delaySecs && ++timeElapsed >= delayEvents && BlankStatus != MUST_BLANK )
- {
- BlankStatus = MUST_BLANK;
- Signal( thisTask, timerSignal );
- }
- }
- else
- {
- timeElapsed = 0;
- if ( BlankStatus != MUST_RETRY )
- {
- BlankStatus = MUST_RETRY;
- Signal( thisTask, handlerSignal );
- }
- }
- }
-
-
- extern char wname[100];
-
- void main( int argc, char *argv[] )
- {
- struct ClientNode *firstNode;
- CxObj *customobj;
- char hotkey[100];
- char ListName[100];
- struct List *list;
- UWORD len;
-
- if ( IntuitionBase = (struct IntuitionBase *)OpenLibrary( "intuition.library", 37L ) )
- {
- if ( UtilityBase = OpenLibrary( "utility.library", 37L ) )
- {
- if ( argc )
- {
- struct RDArgs *rdargs;
- LONG argres[8] = { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L };
-
- if ( rdargs = ReadArgs( "CX_PRIORITY/K/N,CX_POPUP/S,CX_HOTKEY/K,INACTIVE/S,RANDOM/S,TIMEOUT/K/N,DISPLAY/K,LIST/K", argres, NULL ) )
- {
- newbroker.nb_Pri = ( argres[0] ? *(ULONG *)argres[0] : 0 );
- PopUp = ( argres[1] ? TRUE : FALSE );
- strcpy( hotkey, ( argres[2] ? (char *)argres[2] : "lalt b" ) );
- CommodityActive = ( argres[3] ? FALSE : TRUE );
- RandomClient = ( argres[4] ? TRUE : FALSE );
- delaySecs = ( argres[5] ? *(ULONG *)argres[5] : 60 );
- if ( argres[6] )
- strcpy( DefaultModeName, (char *)argres[6] );
- strcpy( ListName, ( argres[7] ? (char *)argres[7] : "ClientList" ) );
- FreeArgs( rdargs );
- }
- }
- else
- if ( IconBase = OpenLibrary( "icon.library", 37L ) )
- {
- char **tooltypes = (char **)ArgArrayInit( argc, argv );
- newbroker.nb_Pri = ArgInt( tooltypes, "CX_PRIORITY", 0 );
- strcpy( hotkey, ArgString( tooltypes, "CX_HOTKEY", "lalt b" ) );
- delaySecs = ArgInt( tooltypes, "TIMEOUT", 3 );
- strcpy( ListName, ArgString( tooltypes, "LIST", "ClientList" ) );
-
- PopUp =
- !Stricmp(ArgString(tooltypes, "CX_POPUP", "NO"), "YES" );
- CommodityActive =
- !Stricmp(ArgString(tooltypes, "ACTIVE", "YES"), "YES" );
- RandomClient =
- !Stricmp(ArgString(tooltypes, "RANDOM", "YES"), "YES" );
- strcpy( DefaultModeName, ArgString( tooltypes, "DISPLAY", NULL ) );
-
- ArgArrayDone();
- CloseLibrary( IconBase );
- }
- CloseLibrary( UtilityBase );
- }
-
- if ( list = CreateModeList() )
- {
- GetDisplayNodeFromName();
- DeleteModeList();
- }
-
- delayEvents = delaySecs * 10;
-
- if ( CxBase = OpenLibrary( "commodities.library", 37L ) )
- {
- if ( GadToolsBase = OpenLibrary( "gadtools.library", 0L ) )
- {
- if ( broker_mp = CreateMsgPort() )
- {
- CXSignal = 1L << broker_mp->mp_SigBit;
- newbroker.nb_Port = broker_mp;
-
- if ( broker = CxBroker( &newbroker, NULL ) )
- {
- if ( (handlerSigBit = AllocSignal( -1 )) != -1 )
- {
- handlerSignal = 1L << handlerSigBit;
-
- if ( (timerSigBit = AllocSignal( -1 )) != -1 )
- {
- timerSignal = 1L << timerSigBit;
-
- thisTask = FindTask( NULL );
-
- if ( customobj = CxCustom( BlankerAction, 0L ) )
- {
- AttachCxObj( broker, customobj );
-
- if (hotkeyfilter = HotKey(hotkey, broker_mp, 1L ) )
- {
- AttachCxObj( broker, hotkeyfilter );
- strcpy(wname, PROGRAMNAME": HotKey = <");
- len = sizeof(PROGRAMNAME": HotKey = <") - 1;
- strncpy(wname + len, hotkey, 100 - len);
- len = strlen(wname);
- strncpy(wname + len, ">", 100 - len);
- }
-
- if ( !CxObjError( hotkeyfilter ) )
- {
- if ( firstNode = AllocVec( sizeof(struct ClientNode), MEMF_CLEAR ) )
- {
- NewList( &ClientsList );
- firstNode->cn_Node.ln_Name = "Builtin blanker";
- AddTail( &ClientsList, (struct Node *)firstNode );
-
- if ( SetUpServerPort() )
- {
- GetClientNames( ListName );
-
- if ( PopUp )
- PopUpWindow();
-
- ActivateCxObj( broker, 1L );
- ProcessUserMessages();
- ShutWindow();
- DropClientNames();
- DeletePort( ServerPort );
- }
- else
- FreeVec( firstNode );
- }
- }
- }
- FreeSignal( timerSigBit );
- }
- FreeSignal( handlerSigBit );
- }
- DeleteCxObjAll( broker );
- }
- DeleteMsgPort( broker_mp );
- }
- CloseLibrary( GadToolsBase );
- }
- CloseLibrary( CxBase );
- }
- CloseLibrary( (struct Library *)IntuitionBase );
- }
- }
-